package com.sc.nft.dao;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dto.CollectionBasicsInfoDTO;
import com.sc.nft.dto.SimpleDaoDTO;
import com.sc.nft.dto.SimpleDigitalCollectionDTO;
import com.sc.nft.entity.CycleCollection;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.SpecialEventManagementBinding;
import com.sc.nft.entity.dto.AccessToCopyrightCollectioDTO;
import com.sc.nft.entity.dto.CollectionListAccordingDaoDTO;
import com.sc.nft.entity.dto.CollectionListAdminDTO;
import com.sc.nft.entity.dto.DigitalcollectionByUserCreatorIdDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.metaWalletDTO.ToMetaCollectionPageDTO;
import com.sc.nft.enums.*;
import com.sc.nft.mapper.DigitalCollectionMapper;
import com.sc.nft.pool.UserPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 藏品表(`sc_digital_collection`)数据DAO
 *
 * @author zxy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-06-03 15:46:04
 */
@Slf4j
@Repository
public class DigitalCollectionDao extends ServiceImpl<DigitalCollectionMapper, DigitalCollection> {
    @Resource
    private SpecialEventManagementBindingDao specialEventManagementBindingDao;
    @Resource
    private NftRegionBindingDao nftRegionBindingDao;

    @Autowired
    private StaticDataDao staticDataDao;

    public DigitalCollection getEcologyCollectionById(Long collectionId) {
        return lambdaQuery().eq(DigitalCollection::getId, collectionId).eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.ECOLOGY).one();
    }

    public List<DigitalCollection> getEcologyCollectionByUserCreatorId(Long userCreatorId) {
        return lambdaQuery().eq(DigitalCollection::getUserCreatorId, userCreatorId).eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.ECOLOGY).list();
    }


    public List<DigitalCollection> getEcologyCollectionSimpleList() {
        return lambdaQuery().select(DigitalCollection::getId, DigitalCollection::getFirstTitle, DigitalCollection::getCreationContent).eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.ECOLOGY).list();
    }

    public Integer getUserMaxLevel(Long userId) {
        return baseMapper.getUserMaxLevel(userId);
    }

    public DigitalCollection getByTitle(String firstTitle) {
        return new LambdaQueryChainWrapper<DigitalCollection>(baseMapper).eq(DigitalCollection::getFirstTitle, firstTitle).one();
    }

    public Page<DigitalCollectionVO> page(String firstTitle, Long userCreatorId, Long catenaId, int pageNo, int pageSize, DigitalMarketTypeEnum collectionType, Long searchId) {
        return baseMapper.page(new Page(pageNo, pageSize), firstTitle, userCreatorId, catenaId, collectionType.getCode(), searchId);

    }

    public List<DigitalCollection> getByIds(List<Long> linkIds) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<DigitalCollection>();
        wrapper.in(DigitalCollection::getId, linkIds);
        return list(wrapper);
    }

    public Page<DigitalCollection> simpleList(Long userCreatorId, Page page) {
        String filterDaoIds = staticDataDao.getByType("filter_dao_id").getValue();
        JSONArray array = JSONArray.parseArray(filterDaoIds);
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<DigitalCollection>();
        wrapper.select(DigitalCollection::getId, DigitalCollection::getFirstTitle, DigitalCollection::getCreationContent, DigitalCollection::getContractAddress, DigitalCollection::getIssuePrice, DigitalCollection::getSumCount, DigitalCollection::getCreateTime);
        wrapper.eq(ObjectUtil.isNotEmpty(userCreatorId) && !array.contains(userCreatorId.longValue()), DigitalCollection::getUserCreatorId, userCreatorId);
        wrapper.orderByAsc(DigitalCollection::getId);
        return page(page, wrapper);
    }

    public Page<DigitalCollection> simpleSearchList(Long userCreatorId, String collectionName, Page page) {
        String filterDaoIds = staticDataDao.getByType("filter_dao_id").getValue();
        JSONArray array = JSONArray.parseArray(filterDaoIds);
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<DigitalCollection>();
        wrapper.select(DigitalCollection::getId, DigitalCollection::getFirstTitle, DigitalCollection::getCreationContent, DigitalCollection::getContractAddress, DigitalCollection::getIssuePrice, DigitalCollection::getSumCount, DigitalCollection::getCreateTime);
        wrapper.eq(ObjectUtil.isNotEmpty(userCreatorId) && !array.contains(userCreatorId.longValue()), DigitalCollection::getUserCreatorId, userCreatorId);
        wrapper.like(StrUtil.isNotBlank(collectionName), DigitalCollection::getFirstTitle, collectionName);
        wrapper.orderByAsc(DigitalCollection::getId);
        return page(page, wrapper);
    }

    public List<DigitalCollection> simpleList(String firstTitle, Long userCreatorId) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<DigitalCollection>();
        wrapper.select(DigitalCollection::getId, DigitalCollection::getFirstTitle, DigitalCollection::getIssuePrice, DigitalCollection::getCreationContent);
        wrapper.like(StrUtil.isNotBlank(firstTitle), DigitalCollection::getFirstTitle, firstTitle);
        wrapper.eq(ObjectUtil.isNotEmpty(userCreatorId), DigitalCollection::getUserCreatorId, userCreatorId);
        wrapper.orderByAsc(DigitalCollection::getId);
        return list(wrapper);
    }

    public DigitalCollection getPrivilegeLevel(Integer privilegeLevel) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<DigitalCollection>();
        wrapper.eq(DigitalCollection::getPrivilegeLevel, privilegeLevel);
        wrapper.eq(DigitalCollection::getIsPrivilege, Boolean.TRUE);
        return getOne(wrapper);
    }

    public Page<PageSynthesisCollectionVO> pageSynthesisCollection(int pageNo, int pageSize, String firstTitle, Boolean isPrivilege) {
        return baseMapper.pageSynthesisCollection(new Page(pageNo, pageSize), firstTitle, isPrivilege);
    }

    public Page<CollectionSimpleVO> pageRelatedCollections(String firstTitle, int pageNo, int pageSize) {
        return baseMapper.pageRelatedCollections(new Page(pageNo, pageSize), firstTitle);
    }

    public Boolean updateNumbering(Long collectionId) {
        UpdateWrapper<DigitalCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", collectionId);
        wrapper.setSql("numbering=numbering + " + 1);
        return update(wrapper);
    }

    public Boolean collectionStockBack(Long relationId, int count) {
        UpdateWrapper<DigitalCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", relationId);
        wrapper.setSql("usable_count=usable_count +" + count);
        return update(wrapper);
    }

    public Boolean uodateCollectionStock(Long relationId, Integer sumCount) {
        UpdateWrapper<DigitalCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", relationId);
        wrapper.setSql("usable_count=usable_count -" + sumCount);
        return update(wrapper);
    }

    public String getFirstTitleById(Long id) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(DigitalCollection::getFirstTitle);
        wrapper.eq(DigitalCollection::getId, id);
        return getObj(wrapper, x -> x.toString());
    }

    public DigitalCollection getByFirstTitle(String firstTitle) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DigitalCollection::getFirstTitle, firstTitle);
        return getOne(wrapper);
    }

    public Boolean newDealSave(Long collectionId, BigDecimal money) {
        UpdateWrapper<DigitalCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", collectionId);
        wrapper.setSql("new_deal=" + money);
        return update(wrapper);
    }

    public PositionStatisticalVO positionStatistical() {
        return baseMapper.positionStatistical(UserPool.getUser().getId());
    }

    public BigDecimal iAssets() {
        return baseMapper.iAssets(UserPool.getUser().getId());
    }


    public Page<AdminDigitalCollectionVO> getListByAdminPage(CollectionListAdminDTO collectionListAdminDTO) {
        return baseMapper.getListByAdminPage(collectionListAdminDTO.toMp(), collectionListAdminDTO.getNewOrOld());
    }

    public Page<HomePageDigitalCollectionProductVO> digitalcollectionByUserCreatorId(DigitalcollectionByUserCreatorIdDTO digitalcollectionByUserCreatorId) {
        return baseMapper.digitalcollectionByUserCreatorId(digitalcollectionByUserCreatorId.toMp(), digitalcollectionByUserCreatorId.getUserCreatorId(), digitalcollectionByUserCreatorId.getIsIosShow(), digitalcollectionByUserCreatorId.getStatus());
    }

    /**
     * 后台获取dao发行藏品数据
     *
     * @param id
     * @return
     */
    public List<GetAdminCollectionVO> getCollectionList(Long id) {
        return baseMapper.getCollectionList(id);
    }


    public boolean updateChainStatus(Long id, CollectionChainStatusEnum chainStatusEnum) {
        UpdateWrapper<DigitalCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("chain_status", chainStatusEnum.getCode());
        return update(wrapper);
    }

    public List<DigitalCollection> findListByChainStatus(CollectionChainStatusEnum chainStatus) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DigitalCollection::getChainStatus, chainStatus);
        return list(wrapper);
    }

    public Page<CollectionListAccordingDaoVO> getCollectionListAccordingDao(CollectionListAccordingDaoDTO collectionListAccordingDaoDTO) {

        return baseMapper.getCollectionListAccordingDao(collectionListAccordingDaoDTO.toMp(), collectionListAccordingDaoDTO.getDaoId());
    }

    public Page<AdminPageFreeMarketCollectionVO> getAdminPageFreeMarketCollection(Integer pageNo, Integer pageSize, String firstTitle) {
        return baseMapper.getAdminPageFreeMarketCollection(new Page(pageNo, pageSize), firstTitle);
    }


    public Boolean updateDeployTransactionHashAndContractStatusById(Long id, String transactionHash, CollectionContractStatusEnum collectionContractStatus) {
        LambdaUpdateWrapper<DigitalCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DigitalCollection::getId, id);
        wrapper.set(DigitalCollection::getDeployTransactionHash, transactionHash);
        wrapper.set(DigitalCollection::getContractStatus, collectionContractStatus);
        return update(wrapper);
    }

    public DigitalCollection getOneByDeployTransactionHash(String transactionHash) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DigitalCollection::getDeployTransactionHash, transactionHash);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }

    public Boolean updateContractAddressAndContractStatusById(Long id, String contractAddress, String deployTransactionHash, CollectionContractStatusEnum collectionContractStatusEnum) {
        LambdaUpdateWrapper<DigitalCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DigitalCollection::getId, id);
        wrapper.set(DigitalCollection::getContractAddress, contractAddress);
        wrapper.set(DigitalCollection::getDeployTransactionHash, deployTransactionHash);
        wrapper.set(DigitalCollection::getContractStatus, collectionContractStatusEnum);
        return update(wrapper);
    }

    public Boolean resetCollectionChainStatusAndContractStatus() {
        LambdaUpdateWrapper<DigitalCollection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(DigitalCollection::getChainStatus, CollectionChainStatusEnum.WAIT_CHAIN);
        wrapper.set(DigitalCollection::getContractStatus, CollectionContractStatusEnum.INIT);
        wrapper.set(DigitalCollection::getContractAddress, StrUtil.EMPTY);
        wrapper.set(DigitalCollection::getDeployTransactionHash, StrUtil.EMPTY);
        return update(wrapper);
    }

    public List<DigitalCollection> getListByChainStatusAndContractStatus(CollectionChainStatusEnum collectionChainStatusEnum, CollectionContractStatusEnum collectionContractStatusEnum) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DigitalCollection::getChainStatus, collectionChainStatusEnum);
        wrapper.eq(DigitalCollection::getContractStatus, collectionContractStatusEnum);
        return list(wrapper);
    }

    public boolean addUsableAountById(Long id, int count) {
        UpdateWrapper<DigitalCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.setSql("usable_count=usable_count +" + count);
        return update(new DigitalCollection(), wrapper);
    }

    public Page<CollectionSimpleVO> getAdminPageCollection(Integer pageNo, Integer pageSize, String firstTitle) {
        return baseMapper.getPageCollection(new Page<>(pageNo, pageSize), firstTitle);
    }

    /**
     * 专场管理绑定藏品的时候藏品查询列表,排除掉已经被绑定的藏品.
     *
     * @return
     */
    public List<AccessToCopyrightCollectionVO> accessToCollection(AccessToCopyrightCollectioDTO accessToCopyrightCollectioDTO) {
        LambdaQueryWrapper<DigitalCollection> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.COPYRIGHT);
        List<SpecialEventManagementBinding> list = specialEventManagementBindingDao.list();
        lambdaQueryWrapper.notIn(CollUtil.isNotEmpty(list), DigitalCollection::getId, list.stream().map(SpecialEventManagementBinding::getCollectionId).collect(Collectors.toList()));
        lambdaQueryWrapper.likeRight(StrUtil.isNotBlank(accessToCopyrightCollectioDTO.getFirstTitle()), DigitalCollection::getFirstTitle, accessToCopyrightCollectioDTO.getFirstTitle());
        return BeanUtil.copyToList(baseMapper.selectList(lambdaQueryWrapper), AccessToCopyrightCollectionVO.class);
    }

    public List<AccessToCopyrightCollectionVO> collectionBindingCity(AccessToCopyrightCollectioDTO accessToCopyrightCollectioDTO) {
        LambdaQueryWrapper<DigitalCollection> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.COPYRIGHT);
        lambdaQueryWrapper.likeRight(StrUtil.isNotBlank(accessToCopyrightCollectioDTO.getFirstTitle()), DigitalCollection::getFirstTitle, accessToCopyrightCollectioDTO.getFirstTitle());
        return BeanUtil.copyToList(baseMapper.selectList(lambdaQueryWrapper), AccessToCopyrightCollectionVO.class);

    }

    public Page<CollectionSimpleVO> getCollectionPageByCollectionType(Integer pageNo, Integer pageSize) {
        return baseMapper.getCollectionPageByCollectionType(new Page<>(pageNo, pageSize));
    }

    public Page<SimpleListByCollectionTypeVO> simpleListByCollectionType(Integer collectionType, String firstTitle, Integer pageNo, Integer pageSize, Long daoId) {
        return baseMapper.simpleListByCollectionType(new Page<>(pageNo, pageSize), collectionType, firstTitle, daoId);
    }

    public List<DigitalCollection> listByUserCreatorId(Long daoId) {
        LambdaQueryWrapper<DigitalCollection> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(DigitalCollection::getUserCreatorId, daoId);
        return baseMapper.selectList(lambdaQueryWrapper);
    }


    public DigitalCollection getOneByIdAndDaoId(Long collectionId, Long daoId) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DigitalCollection::getId, collectionId);
        wrapper.eq(DigitalCollection::getUserCreatorId, daoId);
        return getOne(wrapper);
    }

    public CollectionBasicsInfoDTO getBasicsInfoById(Long id) {
        return baseMapper.getBasicsInfoById(id);
    }

    public SimpleDaoDTO getDaoInfoByCollectionId(Long collectionId) {
        return baseMapper.getDaoInfoByCollectionId(collectionId);
    }

//    public List<Map<String, Integer>> getAllUsableCount(Long id) {
//        return baseMapper.getAllUsableCount(id);
//    }


    public List<CycleCollection> artList(Integer page, Integer pageSize) {
        return baseMapper.artList(new Page<>(page, pageSize)).getRecords();
    }

    public List<ZhiTanArtListVO> getArtList() {
        // 备注：涉及 sc_second_collection 表查询，内置条件 status=1，不必修改
        return baseMapper.getArtList();
    }

    public CycleCollection artInfo(Integer collectionId) {
        return baseMapper.artInfo(collectionId.toString());
    }

    public Integer updateUsableStock(Long id, Boolean isAdd, Integer sumCount) {
        if (isAdd) {
            return baseMapper.addUsableStock(id, sumCount);
        } else {
            return baseMapper.subUsableStock(id, sumCount);
        }
    }

    public Integer subUsableStock(Long id, Integer number) {
        return baseMapper.subUsableStock(id, number);
    }

    /**
     * 根据ids获取对应的藏品信息
     *
     * @param ids
     * @return
     */
    public List<SaasTaskDetailsCollectionVO> getCollectionSimplenessByIds(List<Long> ids) {
        return baseMapper.getCollectionSimplenessByIds(ids);
    }

    public IPage<CollectionSimpleVO> collectionSimpleListPage(String firstTitle, Long userCreatorId, Page mp, SaasTaskTypeEnum typeEnum) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<DigitalCollection>();
        wrapper.select(DigitalCollection::getId, DigitalCollection::getFirstTitle);
        if (typeEnum != null) {
            switch (typeEnum) {
                case COMPOSITE_COLLECTION:
                    wrapper.eq(DigitalCollection::getIsSynthesis, Boolean.TRUE);
                    break;
                case SMELTING_COLLECTION:
                case SMELTING_APPOINT_COLLECTION:
                case SMELTING_WILL_COLLECTION:
                case PASSIVITY_INVITE_SMELTING_APPOINT_COLLECTION:
                case PASSIVITY_INVITE_SMELTING_WILL_COLLECTION:
                    wrapper.eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.ECOLOGY);
                    break;
                default:
                    break;
            }
        }
        wrapper.like(StrUtil.isNotBlank(firstTitle), DigitalCollection::getFirstTitle, firstTitle);
        wrapper.eq(ObjectUtil.isNotEmpty(userCreatorId), DigitalCollection::getUserCreatorId, userCreatorId);
        wrapper.orderByDesc(DigitalCollection::getId);
        return page(mp, wrapper);
    }

    public List<CycleCollection> largeDiskAccess() {
        return baseMapper.largeDiskAccess();
    }

    public List<LargeDiskAccessVO> getLargeDiskAccessDataNotEcology() {
        return baseMapper.getLargeDiskAccessDataNotEcology();
    }

    public List<LargeDiskAccessVO> getLargeDiskAccessDataInEcology() {
        return baseMapper.getLargeDiskAccessDataInEcology();
    }

    public List<Long> getEcologyCollectionIdList() {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper();
        wrapper.select(DigitalCollection::getId);
        wrapper.eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.ECOLOGY);

        return listObjs(wrapper, (x) -> (Long) x);
    }

    public IPage<SimpleDigitalCollectionDTO> getEcologyCollection(Long daoId, String collectionName, Page page) {
        return baseMapper.getEcologyCollection(page, daoId, collectionName);
    }

    public List<SimpleListByCollectionTypeVO> simpleListByCollectionAll(SaasTaskTypeEnum typeEnum) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<DigitalCollection>();
        wrapper.select(DigitalCollection::getId, DigitalCollection::getFirstTitle);
        if (BeanUtil.isNotEmpty(typeEnum)) {
            switch (typeEnum) {
                case COMPOSITE_COLLECTION:
                    wrapper.eq(DigitalCollection::getIsSynthesis, Boolean.TRUE);
                    break;
                case SMELTING_COLLECTION:
                case SMELTING_APPOINT_COLLECTION:
                case SMELTING_WILL_COLLECTION:
                case PASSIVITY_INVITE_SMELTING_APPOINT_COLLECTION:
                case PASSIVITY_INVITE_SMELTING_WILL_COLLECTION:
                    wrapper.eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.ECOLOGY);
                    break;
                default:
                    break;
            }
        }
        wrapper.orderByDesc(DigitalCollection::getId);
        List<DigitalCollection> list = list(wrapper);
        List<SimpleListByCollectionTypeVO> listByCollectionTypeVOS = new ArrayList<>();
        for (DigitalCollection digitalCollection : list) {
            SimpleListByCollectionTypeVO simpleListByCollectionTypeVO = new SimpleListByCollectionTypeVO();
            simpleListByCollectionTypeVO.setCollectionName(digitalCollection.getFirstTitle());
            simpleListByCollectionTypeVO.setId(digitalCollection.getId());
            listByCollectionTypeVOS.add(simpleListByCollectionTypeVO);
        }
        return listByCollectionTypeVOS;
    }

    public DigitalCollection getCollectionByIdAndIsGiveImeta(Long id, Boolean isGiveImeta) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DigitalCollection::getId, id);
        wrapper.eq(DigitalCollection::getIsGiveImeta, isGiveImeta);
        return getOne(wrapper);
    }

    public Page<ToMetaCollectionPageDTO> toWalletNotLinkCollectionPage(String daoName, String collectioName, Page page) {
        return baseMapper.toWalletNotLinkCollectionPage(page, daoName, collectioName);
    }

    public List<Long> getEquityId(DaoLevelTypeEnum daoLevelTypeEnum) {
        LambdaQueryWrapper<DigitalCollection> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollection::getIsUpgrade, Boolean.TRUE);
        wrapper.eq(DigitalCollection::getDaoLevel, daoLevelTypeEnum);
        List<DigitalCollection> list = list(wrapper);
        return list.stream().map(DigitalCollection::getId).collect(Collectors.toList());
    }

    public List<DigitalCollection> hatchCollectionList(String collectionName, Long daoId) {
        LambdaQueryWrapper<DigitalCollection> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(collectionName), DigitalCollection::getFirstTitle, collectionName);
        wrapper.eq(Objects.nonNull(daoId) && !Objects.equals(daoId, 6L), DigitalCollection::getUserCreatorId, daoId);
        wrapper.eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.ECOLOGY);
        wrapper.orderByDesc(DigitalCollection::getId);
        return list(wrapper);
    }

    public List<DigitalCollection> getListByDaoIds(List<Long> daoIds) {
        LambdaQueryWrapper<DigitalCollection> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollection::getCollectionType, DigitalMarketTypeEnum.ECOLOGY);
        wrapper.in(DigitalCollection::getUserCreatorId, daoIds);
        return list(wrapper);
    }


    public List<DigitalCollection> findListByDigitalMarketType(DigitalMarketTypeEnum digitalMarketTypeEnum) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DigitalCollection::getCollectionType, digitalMarketTypeEnum);
        wrapper.eq(DigitalCollection::getIsDelete, Boolean.FALSE);
        return list(wrapper);
    }

    /**
     * 仅供后台使用，前台业务禁止使用
     *
     * @param keyword 藏品名称关键词
     */
    public List<DigitalCollection> findByKeyword(String keyword) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(keyword), DigitalCollection::getFirstTitle, keyword);
        wrapper.eq(DigitalCollection::getIsDelete, Boolean.FALSE);
        return list(wrapper);
    }

    /**
     * 按照品牌及流通类型查询
     *
     * @param daoId
     * @param circulationType
     * @param collectionName
     * @return
     */
    public List<DigitalCollection> listByDaoCirculationType(Long daoId, CirculationTypeEnum circulationType, String collectionName) {
        LambdaQueryWrapper<DigitalCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DigitalCollection::getUserCreatorId, daoId);
        wrapper.eq(DigitalCollection::getCirculationType, circulationType);
        wrapper.like(StrUtil.isNotBlank(collectionName), DigitalCollection::getFirstTitle, collectionName);
        wrapper.eq(DigitalCollection::getIsDelete, Boolean.FALSE);
        return list(wrapper);
    }

    /**
     * 减去商品库存.
     *
     * @param id
     * @param sumCount
     */
    public Boolean subSumCount(Long id, int sumCount) {
        return baseMapper.subSumCount(id, sumCount) > 0;
    }
}